<?php
/**
 * Created by PhpStorm.
 * User: mengkang <i@mengkang.net>
 * Date: 2019/10/2 下午6:59
 */

class Url{
    private $url;
    private $uri;
    private $host;
    private $ip;
    private $port = 80;


    public function __construct($url)
    {
        $this->url = $url;
        $urlInfo = parse_url($url);

        if (!isset($urlInfo["host"])) {
            throw new RuntimeException("非法的 url");
        }

        $this->host = $urlInfo["host"];

        $this->ip = gethostbyname($this->host);

        $this->uri = $urlInfo["path"];

        if (isset($urlInfo["port"])) {
            $this->port = $urlInfo["port"];
        }

        $this->uri = $urlInfo["path"];
        if (isset($urlInfo["query"])) {
            $this->uri .= $this->uri . "?" . $urlInfo["query"];
        }
    }

    /**
     * @return mixed
     */
    public function getUri()
    {
        return $this->uri;
    }

    /**
     * @return mixed
     */
    public function getHost()
    {
        return $this->host;
    }

    /**
     * @return mixed
     */
    public function getIp()
    {
        return $this->ip;
    }

    /**
     * @return mixed
     */
    public function getPort()
    {
        return $this->port;
    }
}